package com.example.firstdemo.ui.screens.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.example.firstdemo.ui.theme.AppTheme
import com.example.firstdemo.ui.theme.FontSize
import com.example.firstdemo.ui.theme.Space
import com.example.firstdemo.ui.theme.eColors

@Composable
fun Dialog(
    title: String = "",
    info: String = "",
    confirmText: String = "确定",
    dismissText: String = "取消",
    onDismiss: () -> Unit = {},
    onConfirm: () -> Unit = {},
) {
    Dialog(onDismissRequest = onDismiss) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight(),
            shape = RoundedCornerShape(16.dp),
        ) {
            Column(modifier = Modifier.padding(Space.medium)) {
                Text(
                    text = title,
                    color = MaterialTheme.eColors.text.regular,
                    fontSize = FontSize.xl,
                    fontWeight = FontWeight.W600
                )
                Spacer(modifier = Modifier.height(Space.small))
                Box(
                    contentAlignment = Alignment.CenterStart,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Text(
                        text = info,
                        fontSize = FontSize.large,
                        color = MaterialTheme.eColors.text.secondary,
                    )
                }
//                Spacer(modifier = Modifier.height(Space.small))
                Row(
                    horizontalArrangement = Arrangement.End,
                    modifier = Modifier
                        .fillMaxWidth()
                        .wrapContentHeight()
                        .padding(0.dp),
                    verticalAlignment = Alignment.Top
                ) {
                    TextButton(
                        onClick = onDismiss,
                        modifier = Modifier.padding(0.dp)
                    ) {
                        Text(dismissText)
                    }
                    Spacer(modifier = Modifier.width(Space.small))
                    TextButton(
                        onClick = onConfirm,
                        contentPadding = PaddingValues(5.dp),
                        modifier = Modifier
                            .defaultMinSize(1.dp, 1.dp)
                            .padding(0.dp)
                    ) {
                        Text(confirmText)
                    }
                }
            }
        }
    }
}

@Preview(
    showBackground = true,
    showSystemUi = false
)
@Composable
fun DialogPreview() {
    AppTheme() {
        Dialog(
            title = "自定义对话框",
            info = "这是一个完全自定义的对话框。",
            confirmText = "确定",
            dismissText = "取消",
            onDismiss = {},
            onConfirm = {},
        )
    }
}